package com.ruoyi.classai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.classai.domain.EwcResourceDetails;
import com.ruoyi.classai.domain.dto.resource.EwcResourceDetailsAddDto;
import com.ruoyi.classai.domain.dto.resource.EwcResourceDetailsQueryDto;
import com.ruoyi.classai.domain.vo.resource.EwcResourceDetailsVo;
import com.ruoyi.classai.mapper.EwcResourceDetailsMapper;
import com.ruoyi.classai.service.IEwcResourceDetailsService;
import com.ruoyi.classai.utils.FileUtil;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 资源详情信息Service业务层处理
 *
 * @author lcj
 * @date 2025-03-16
 */
@Service
public class EwcResourceDetailsServiceImpl extends ServiceImpl<EwcResourceDetailsMapper, EwcResourceDetails> implements IEwcResourceDetailsService {


    @Autowired
    private EwcResourceDetailsMapper detailsMapper;

    /**
     * 分页查询资源详情信息列表
     */
    @Override
    public List<EwcResourceDetails> queryList(EwcResourceDetailsQueryDto dto) {
        LambdaQueryWrapper<EwcResourceDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dto.getResourceId()), EwcResourceDetails::getResourceId, dto.getResourceId())
                .like(StringUtils.isNotBlank(dto.getResourceName()), EwcResourceDetails::getResourceName, dto.getResourceName())
                .eq(dto.getFileType() != null, EwcResourceDetails::getFileType, dto.getFileType())
                .orderByDesc(EwcResourceDetails::getCreateTime);
        return this.list(queryWrapper);
    }

    /**
     * 导出资源详情信息列表
     */
    @Override
    public void export(HttpServletResponse response, EwcResourceDetails ewcResourceDetails) {
        List<EwcResourceDetails> list = this.list(Wrappers.query(ewcResourceDetails));
        ExcelUtil<EwcResourceDetails> util = new ExcelUtil<EwcResourceDetails>(EwcResourceDetails.class);
        util.exportExcel(response, list, "资源详情信息数据");
    }

    /**
     * 获取资源详情信息详细信息
     */
    @Override
    public EwcResourceDetailsVo getInfo(String id) {
        EwcResourceDetails details = this.getById(id);
        if (details == null) {
            throw new GlobalException("资源详情不存在");
        }
        EwcResourceDetailsVo vo = new EwcResourceDetailsVo();
        BeanUtils.copyProperties(details, vo);
        return vo;
    }

    /**
     * 新增资源详情信息
     */
    @Override
    public void add(EwcResourceDetailsAddDto dto) {
        EwcResourceDetails details = new EwcResourceDetails();
        BeanUtils.copyProperties(dto, details);

        // 处理文件后缀和类型
        if (StringUtils.isNotBlank(dto.getResourceUrl())) {
            String resourceSuffix = FileUtil.getResourceSuffix(dto.getResourceUrl());
            details.setFileDetails(resourceSuffix);
            details.setFileType(FileUtil.getFileType(resourceSuffix));
        }

        this.save(details);
    }

    /**
     * 修改资源详情信息
     */
    @Override
    public void edit(EwcResourceDetailsAddDto dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new GlobalException("ID不能为空");
        }

        EwcResourceDetails details = this.getById(dto.getId());
        if (details == null) {
            throw new GlobalException("资源详情不存在");
        }

        BeanUtils.copyProperties(dto, details);

        // 处理文件后缀和类型
        if (StringUtils.isNotBlank(dto.getResourceUrl())) {
            String resourceSuffix = FileUtil.getResourceSuffix(dto.getResourceUrl());
            details.setFileDetails(resourceSuffix);
            details.setFileType(FileUtil.getFileType(resourceSuffix));
        }
        details.setUpdateTime(new Date());

        this.updateById(details);
    }

    /**
     * 删除资源详情信息
     */
    @Override
    public void remove(String[] ids) {
        List<String> idList = Arrays.asList(ids);
        this.removeByIds(idList);
    }

    /**
     * 查询资源详情信息列表（不分页）
     */
    @Override
    public List<EwcResourceDetailsVo> resourceDetailsList(EwcResourceDetailsQueryDto dto) {
        return detailsMapper.getResourceDetailsList(dto.getGradeType(), dto.getSubjectType(), dto.getResourceName(), dto.getResourceId(), dto.getFileType());
    }
}
